"""
小说合理性分析API
"""
import json
from fastapi import UploadFile, File
from service.api_wrap import async_api_wrapper, BaseResponse
from loguru import logger
from core.novel_analyze import NovelAnalyzer
from core.path import upload_dir


@async_api_wrapper
async def novel_analyze_api(
        upload_file: UploadFile = File(...,description='小说文本文档')
):

    contents = await upload_file.read()
    save_file = upload_dir / upload_file.filename
    with open(save_file, 'wb') as fw:
        fw.write(contents)

    analyzer = NovelAnalyzer(save_file)
    novel = analyzer.analyze()

    logger.info(f'分析结论: {json.dumps(novel.__dict__, ensure_ascii=False)}')
    return BaseResponse(data=novel.__dict__)
